package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlOddjobOmember;
import com.ruoyi.sl.domain.SlRwork;
import com.ruoyi.sl.domain.SlSpecial;
import com.ruoyi.sl.service.ISlSpecialService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlRworkRmember;
import com.ruoyi.sl.service.ISlRworkRmemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 操作人Controller
 * 
 * @author admin
 * @date 2024-06-07
 */
@RestController
@RequestMapping("/sl/rmember")
public class SlRworkRmemberController extends BaseController
{
    @Autowired
    private ISlRworkRmemberService slRworkRmemberService;
    @Autowired
    private ISlSpecialService slSpecialService;

    /**
     * 查询操作人列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlRworkRmember slRworkRmember)
    {
        startPage();
        List<SlRworkRmember> list = slRworkRmemberService.selectSlRworkRmemberList(slRworkRmember);
        return getDataTable(list);
    }

    /**
     * 查询操作人列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:list')")
    @GetMapping("/alllist/{rworkId}")
    public AjaxResult listByRworkId(@PathVariable("rworkId") Long rworkId)
    {
        List<SlRworkRmember> list = slRworkRmemberService.selectSlRworkRmemberListByRworkId(rworkId);
        return success(list);
    }


    /**
     * 查询未提交报工项的操作人列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:list')")
    @GetMapping("/notmember")
    public AjaxResult notMemberList(SlRwork slRwork)
    {
        LoginUser loginUser = getLoginUser();
        slRwork.setRworkUser(getLoginUser().getUsername());
        List<SlRworkRmember> list = slRworkRmemberService.selectNotSlRworkRmemberList(slRwork);

        //报工人是不是特殊系人员，特殊系不参与生产但是要填出勤天数
        SlSpecial slSpecial = slSpecialService.selectSlSpecialByUserName(getLoginUser().getUsername());
        if(StringUtils.isNotNull(slSpecial)){
            //不参与生产,出勤人加上他
            if(slSpecial.getIsProduce().equals("1")){
                SlRworkRmember slRworkRmember = new SlRworkRmember();
                slRworkRmember.setRmemberUser(slSpecial.getUserName());
                slRworkRmember.setRmemberNick(slSpecial.getNickName());
                list.add(slRworkRmember);
            }
        }


        return success(list);
    }

    /**
     * 导出操作人列表
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:export')")
    @Log(title = "操作人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlRworkRmember slRworkRmember)
    {
        List<SlRworkRmember> list = slRworkRmemberService.selectSlRworkRmemberList(slRworkRmember);
        ExcelUtil<SlRworkRmember> util = new ExcelUtil<SlRworkRmember>(SlRworkRmember.class);
        util.exportExcel(response, list, "操作人数据");
    }

    /**
     * 获取操作人详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:query')")
    @GetMapping(value = "/{rmemberId}")
    public AjaxResult getInfo(@PathVariable("rmemberId") Long rmemberId)
    {
        return success(slRworkRmemberService.selectSlRworkRmemberByRmemberId(rmemberId));
    }

    /**
     * 新增操作人
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:add')")
    @Log(title = "操作人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlRworkRmember slRworkRmember)
    {
        slRworkRmember.setCreateBy(getUsername());
        return toAjax(slRworkRmemberService.insertSlRworkRmember(slRworkRmember));
    }

    /**
     * 修改操作人
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:edit')")
    @Log(title = "操作人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlRworkRmember slRworkRmember)
    {
        slRworkRmember.setUpdateBy(getUsername());
        return toAjax(slRworkRmemberService.updateSlRworkRmember(slRworkRmember));
    }

    /**
     * 删除操作人
     */
    @PreAuthorize("@ss.hasPermi('sl:rwork:remove')")
    @Log(title = "操作人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rmemberIds}")
    public AjaxResult remove(@PathVariable Long[] rmemberIds)
    {
        return toAjax(slRworkRmemberService.deleteSlRworkRmemberByRmemberIds(rmemberIds));
    }
}
